﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Threading;

namespace WpfApp1
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

       

        private void btnClose_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        private void btnStopExam_Click(object sender, RoutedEventArgs e)
        {
            timer.Stop();
            StopExamFrm frm = new StopExamFrm();
            frm.Show();
            
        }

        private void btnNewRecord_Click(object sender, RoutedEventArgs e)
        {
            NewRecordFrm frm = new NewRecordFrm();
            frm.Show();
        }

        private void btnBedSet_Click(object sender, RoutedEventArgs e)
        {
            BedSetFrm frm = new BedSetFrm();
            frm.Show();
        }

        private void btnMenuLeadSet_Click(object sender, RoutedEventArgs e)
        {
            LeadSetFrm frm = new LeadSetFrm();
            frm.Show();
        }

        private void btnMenuAmp_Click(object sender, RoutedEventArgs e)
        {
            AmpSetFrm frm = new AmpSetFrm();
            frm.Show();

        }

        private void btnMenuSpeedSet_Click(object sender, RoutedEventArgs e)
        {
            SpeedSetFrm frm = new SpeedSetFrm();
            frm.Show();
        }

        private void btnWaveReplay_Click(object sender, RoutedEventArgs e)
        {
            HRV12ECG fm = new HRV12ECG();
            fm.Show();
        }

        private void btnmenuEcg(object sender, RoutedEventArgs e)
        {
            HRVCAL frm = new HRVCAL();
            frm.Show();
        }

        private void DrawECGWavePanel_PreviewMouseRightButtonDown(object sender, MouseButtonEventArgs e)
        {
            //var mousepos = e.GetPosition(null);
            //ContextMenu menu = new ContextMenu();
            //MenuItem item = new MenuItem();
            //item.Header = "走速";
            //MenuItem item1 = new MenuItem();
            //item.Header = "10mm/s";
            //item.Items.Add(item1);
            //menu.Items.Add(item);

            //menu.PlacementTarget =(UIElement) sender;
            //menu.IsOpen = true;
            //menu.Placement = System.Windows.Controls.Primitives.PlacementMode.MousePoint;


        }

        private void btnenugrid(object sender, RoutedEventArgs e)
        {
            WindowGrid frm = new WindowGrid();
            frm.Show();
        }

      
        private WriteableBitmap wBitmapecg;


        private void DrawECG()
        {

            int width = (int)DrawPanelCanvas.ActualWidth;
            int height = (int)DrawPanelCanvas.ActualHeight;
            if (width > 0 && height > 0)
            {
                IamgeMultParams.Width = width;
                IamgeMultParams.Height = height;
                wBitmapecg = new WriteableBitmap(width, height, 72, 72, PixelFormats.Bgr24, null);
                IamgeMultParams.Source = wBitmapecg;
            }
            wBitmapecg.Lock();
            Bitmap backBitmap = new Bitmap(width, height, wBitmapecg.BackBufferStride, System.Drawing.Imaging.PixelFormat.Format24bppRgb, wBitmapecg.BackBuffer);
            Graphics graphics = Graphics.FromImage(backBitmap);
            graphics.Clear(System.Drawing.Color.Gray);
 
          
            var tempcurrentWave= currentWave.Where(s => s.IsShow == true);
            int linecount = tempcurrentWave.Count();
            int xdistance = height / (linecount + 1);


            Font drawFont = new Font("Arial", 8);
            SolidBrush drawBrush = new SolidBrush(System.Drawing.Color.Yellow);
            graphics.DrawString("X1", drawFont, drawBrush, 5, 10);
            int a = 0;
            //设hi
            for (int i = 0; i < currentWave.Count; i++)
            {
                
                if (currentWave[i].IsShow)
                {
                    currentWave[i].Yloc = xdistance * (a + 1);
                    a++;

                    System.Drawing.Pen pen = new System.Drawing.Pen(currentWave[i].waveColor);

                     graphics.DrawString(currentWave[i].name, drawFont, drawBrush, currentWave[i].Xloc-20, currentWave[i].Yloc-5);
                    if (1 < currentDatalist.Count)
                    {
                        for (int  j=0;j< currentDatalist.Count-1;j++)
                        {
                            if (currentWave[i].name == "bed")
                            {
                                graphics.DrawLine(pen, (float)(j * currentWave[i].speed+ currentWave[i].Xloc), (float)(currentWave[i].Yloc - currentDatalist[j].beddata.bedangle), (float)((j + 1) * currentWave[i].speed + currentWave[i].Xloc), (float)(currentWave[i].Yloc  - currentDatalist[j+1].beddata.bedangle));
                            }
                            if (currentWave[i].name == "resp")
                            {
                                graphics.DrawLine(pen, (float)(j * currentWave[i].speed + currentWave[i].Xloc), (float)(currentWave[i].Yloc - currentDatalist[j].breateData.breateValue), (float)((j + 1) * currentWave[i].speed + currentWave[i].Xloc), (float)(currentWave[i].Yloc  - currentDatalist[j+1].breateData.breateValue));
                            }
                            if (currentWave[i].name == "sys")
                            {
                                graphics.DrawLine(pen, (float)(j * currentWave[i].speed + currentWave[i].Xloc), (float)(currentWave[i].Yloc - currentDatalist[j].bpdata.sbpValue), (float)((j + 1) * currentWave[i].speed + currentWave[i].Xloc), (float)(currentWave[i].Yloc   - currentDatalist[j+1].bpdata.sbpValue));
                            }
                            if (currentWave[i].name == "map")
                            {

                                graphics.DrawLine(pen, (float)(j * currentWave[i].speed + currentWave[i].Xloc), (float)(currentWave[i].Yloc - currentDatalist[j].bpdata.mapValue), (float)((j + 1) * currentWave[i].speed + currentWave[i].Xloc), (float)(currentWave[i].Yloc - currentDatalist[j+1].bpdata.mapValue));
                            }
                            if (currentWave[i].name == "dia")
                            {
                                graphics.DrawLine(pen, (float)(j * currentWave[i].speed + currentWave[i].Xloc), (float)(currentWave[i].Yloc - currentDatalist[j].bpdata.dbpValue), (float)((j + 1) * currentWave[i].speed + currentWave[i].Xloc), (float)(currentWave[i].Yloc - currentDatalist[j+1].bpdata.dbpValue));
                            }
                        }
                      
                    }
                }

            }
 
            graphics.Flush();
            graphics.Dispose();
            graphics = null;
            backBitmap.Dispose();
            backBitmap = null;
            wBitmapecg.AddDirtyRect(new Int32Rect(0, 0, width, height));
            wBitmapecg.Unlock();
        }

        private DispatcherTimer timer;

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {

            //添加 波形类 
            string[] wavenames = new string[] {"bed","resp", "sys", "map", "dia" };
            System.Drawing.Color[] waveColos = new System.Drawing.Color[] { System.Drawing.Color.Yellow, System.Drawing.Color.Blue, System.Drawing.Color.Red, System.Drawing.Color.OrangeRed, System.Drawing.Color.DarkRed };
            for (int i=0;i< wavenames.Length ;i++)
            {
                WavePropertyClass bedwave = new WavePropertyClass();
                bedwave.id = 1;
                bedwave.name = wavenames[i];
                bedwave.IsShow = true;
                bedwave.Xloc = 50;
                bedwave.speed = 1;
                bedwave.waveColor = waveColos[i];

                currentWave.Add(bedwave);

            }
           
            timer = new DispatcherTimer();
            timer.Interval = TimeSpan.FromMilliseconds(100);
            timer.Tick += Timer_Tick;
         
        }

        public ObservableCollection<CurrentDataClass> currentDatalist = new ObservableCollection<CurrentDataClass>();
        public List<WavePropertyClass> currentWave = new List<WavePropertyClass>();


        private int timecount = 0;
        
        private void Timer_Tick(object sender, EventArgs e)
        {
            timecount++;
            
            if (timecount>10)
            {
                timecount = -10;
            }
            
            CurrentDataClass currentdata = new CurrentDataClass();

            BedDataClass beddata = new BedDataClass();
            beddata.id = timecount;
            beddata.bedangle = timecount  ;
            beddata.bedstatus = timecount < 15 ? "平躺位" : "倾斜";
           

            BPDataClass bpdata = new BPDataClass();
            bpdata.ID = timecount;
            bpdata.sbpValue =  timecount;
            bpdata.dbpValue =  timecount;

            BreateDataClass breateData = new BreateDataClass();
            breateData.breateValue = timecount;

            currentdata.beddata = beddata;
            currentdata.bpdata = bpdata;
            currentdata.breateData = breateData;
            currentdata.ID = timecount;
            currentdata.Hr =   timecount;
            currentdata.STJ = 1.0 / timecount;
            currentdata.time = DateTime.Now.ToLongTimeString();

            currentDatalist.Add(currentdata);
             
               
            DrawECG();
            
           
        }

        private void btnCheckShow(object sender, RoutedEventArgs e)
        {
            int index= int.Parse(((CheckBox)sender).Tag.ToString()) ;
            bool bb =(bool) ((CheckBox)sender).IsChecked;

            currentWave[index].IsShow = bb;
            DrawECG();

        }

        private void btnStartExam_Click(object sender, RoutedEventArgs e)
        {
            timer.Start();

        }
    }
}
